﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
using gdlmo.DNN.Modules.Article.Business;

namespace gdlmo.DNN.Modules.Article
{
	public abstract class CategoryEdit : PortalModuleBase
	{

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cmdUpdate.Click += new System.EventHandler(this.cmdUpdate_Click);
			this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
			this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region Controls
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		protected System.Web.UI.WebControls.LinkButton cmdCancel;
		protected System.Web.UI.WebControls.LinkButton cmdDelete;
		protected System.Web.UI.WebControls.TextBox txtDescription;
		protected System.Web.UI.WebControls.TextBox txtName;
		protected System.Web.UI.WebControls.RequiredFieldValidator valName;
		protected System.Web.UI.WebControls.CheckBox chkIsActive;
		protected System.Web.UI.HtmlControls.HtmlTable tblAuthorize;
		protected DotNetNuke.UI.UserControls.DualListControl ctlAdminRoles;
		protected DotNetNuke.UI.UserControls.DualListControl ctlEditRoles;
		protected DotNetNuke.UI.UserControls.DualListControl ctlViewRoles;
		#endregion		

		#region Private Members
		private int CategoryID;
		#endregion

		#region "Private Methods"
		private void BindRoles(CategoryInfo objCategoryInfo)
		{
			DotNetNuke.Security.Roles.RoleController ctlRoles = new DotNetNuke.Security.Roles.RoleController();
			ArrayList portalRoles = ctlRoles.GetPortalRoles(PortalId);
			ArrayList AvaiAdminRoles = new ArrayList();
			ArrayList AdminRoles = new ArrayList();
			ArrayList AvaiEditRoles = new ArrayList();
			ArrayList EditRoles = new ArrayList();
			ArrayList AvaiViewRoles = new ArrayList();
			ArrayList ViewRoles = new ArrayList();
			foreach(DotNetNuke.Security.Roles.RoleInfo info in portalRoles)
			{
				AvaiAdminRoles.Add(info);
				AvaiEditRoles.Add(info);
				AvaiViewRoles.Add(info);
			}

			if (objCategoryInfo != null)
			{
				string[] Roles = objCategoryInfo.AdminRoles.Split(new char[] { ';' });				
				foreach(string rolename in Roles)
				{
					if(rolename.Trim().Length>0)					
					{
						DotNetNuke.Security.Roles.RoleInfo info = ctlRoles.GetRoleByName(PortalId, rolename);							
						AdminRoles.Add(info);						
					}
				}
				foreach(DotNetNuke.Security.Roles.RoleInfo info in portalRoles)
				{
					for(int i=0;i<AdminRoles.Count;i++)
					{						
						if(info.RoleID == ((DotNetNuke.Security.Roles.RoleInfo)AdminRoles[i]).RoleID)
						{
							AvaiAdminRoles.Remove(info);
						}
					}
				}
				Roles = objCategoryInfo.EditRoles.Split(new char[] {';'});
				foreach(string rolename in Roles)
				{
					if(rolename.Trim().Length>0)					
					{
						DotNetNuke.Security.Roles.RoleInfo info = ctlRoles.GetRoleByName(PortalId, rolename);	
						EditRoles.Add(info);
					}
				}
				foreach(DotNetNuke.Security.Roles.RoleInfo info in portalRoles)
				{
					for(int i=0;i<EditRoles.Count;i++)
					{						
						if(info.RoleID == ((DotNetNuke.Security.Roles.RoleInfo)EditRoles[i]).RoleID)
						{
							AvaiEditRoles.Remove(info);
						}
					}
				}
				Roles = objCategoryInfo.ViewRoles.Split(new char[] {';'});
				foreach(string rolename in Roles)
				{
					if(rolename.Trim().Length>0)					
					{
						DotNetNuke.Security.Roles.RoleInfo info = ctlRoles.GetRoleByName(PortalId, rolename);					
						ViewRoles.Add(info);
					}
				}
				foreach(DotNetNuke.Security.Roles.RoleInfo info in portalRoles)
				{
					for(int i=0;i<ViewRoles.Count;i++)
					{						
						if(info.RoleID == ((DotNetNuke.Security.Roles.RoleInfo)ViewRoles[i]).RoleID)
						{
							AvaiViewRoles.Remove(info);
						}
					}
				}
			}
			ctlAdminRoles.Available = AvaiAdminRoles;
			ctlAdminRoles.Assigned = AdminRoles;
			ctlAdminRoles.DataBind();
			ctlEditRoles.Available = AvaiEditRoles;
			ctlEditRoles.Assigned = EditRoles;
			ctlEditRoles.DataBind();
			ctlViewRoles.Available = AvaiViewRoles;
			ctlViewRoles.Assigned = ViewRoles;
			ctlViewRoles.DataBind();
		}
		#endregion

		#region Event Handlers
		private void Page_Load(object sender, System.EventArgs e)
		{
			try 
			{	
				if(UserInfo.UserID != -1)
				{
					if(DotNetNuke.Security.PortalSecurity.IsInRole("Administrators") )
					{
						if (!(Request.Params["CategoryID"] == null)) 
						{
							CategoryID = Int32.Parse(Request.Params["CategoryID"]);
						} 
						else 
						{
							CategoryID = Null.NullInteger;
						}
						if (!Page.IsPostBack) 
						{					
							CategoryController objCtlCategory = new CategoryController();
							CategoryInfo objCategory = new CategoryInfo();					
							cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");
							if (!Null.IsNull(CategoryID)) 
							{						
								if (objCategory != null) 
								{
									objCategory = objCtlCategory.Get(CategoryID, ModuleId);
									txtName.Text = objCategory.Name ;
									txtDescription.Text = objCategory.Description;
									chkIsActive.Checked = objCategory.IsActive ;
									BindRoles(objCategory);
									cmdDelete.Visible = true;
								} 
								else 
								{
									Response.Redirect(Globals.NavigateURL(), true);
								}
							}
							else
							{
								BindRoles(null);
							}
						}
					}
				}
				
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			try 
			{
				if (Page.IsValid == true) 
				{
					CategoryInfo objCategory = new CategoryInfo();
//					objCategory = ((CategoryInfo)CBO.InitializeObject(objCategory, typeof(CategoryInfo)));
					objCategory.CategoryID = CategoryID;
					objCategory.ModuleID = ModuleId;
					objCategory.Name = txtName.Text;
					objCategory.Description = txtDescription.Text;
					objCategory.CreatedDate = DateTime.Now;
					objCategory.IsActive = chkIsActive.Checked;
					objCategory.CreatedByUser = UserInfo.UserID.ToString();
					CategoryController objCtlCategory = new CategoryController();
					string roles = string.Empty;
					foreach(ListItem item in ctlAdminRoles.Assigned)
					{
						roles += item.Text + ";";
					}
					objCategory.AdminRoles = roles;
					roles = string.Empty;
					foreach(ListItem item in ctlEditRoles.Assigned)
					{
						roles += item.Text + ";";
					}
					objCategory.EditRoles = roles;
					roles = string.Empty;
					foreach(ListItem item in ctlViewRoles.Assigned)
					{
						roles += item.Text + ";";
					}
					objCategory.ViewRoles = roles;
					if (Null.IsNull(CategoryID)) 
					{
						objCtlCategory.Add(objCategory);
					} 
					else 
					{
						objCtlCategory.Update(objCategory);
					}
					Response.Redirect(Globals.NavigateURL(), true);
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			try 
			{
				Response.Redirect(EditUrl("CategoriesManager"),true);
				
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdDelete_Click(object sender, EventArgs e)
		{
			try 
			{
				if (!Null.IsNull(CategoryID)) 
				{
					CategoryController objCtlCategory = new CategoryController();
					objCtlCategory.Delete(CategoryID);
				}
				Response.Redirect(Globals.NavigateURL(), true);
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		#endregion
	}
}